#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 20
#define SWAP(a, b) \
    do { \
        typeof(a) (t); \
        (t) = (a); \
        (a) = (b); \
        (b) = (t); \
    } while(0)


void bubble_sort(int arr[], int len);

int main(void)
{
    int arr[N];
    srand(time(NULL));
    int i;

    /* 给数组赋值 */
    for (i = 0; i < N; i++) {
        arr[i] = rand() % 1000;
        printf("%d ", arr[i]);
    }
    printf("\n");

    /* 给数组排序 */
    bubble_sort(arr, N);

    for (i = 0; i < N; i++)
        printf("%d ", arr[i]);

    printf("\n");
    return 0;
}

void bubble_sort(int arr[], int len)
{
    /* 实现从小到大排序 */
    int i, j;
    for (i = 0; i < len; i++) {
        for (j = 0; j < len - i - 1; j++) {
            if (arr[j] > arr[j + 1])
                SWAP(arr[j], arr[j + 1]);
        }
    }
    printf("\n");
}


